const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property is_debug: boolean = false;

    private new_draw_node: cc.Graphics = null;
    private road_data_set = [];
    private draw_node: cc.Node = null;

    onLoad() {
        let anim_com = this.node.getComponent(cc.Animation);
        let clips = anim_com.getClips();
        let clip = clips[0];

        let newNode = new cc.Node();
        this.new_draw_node = newNode.getComponent(cc.Graphics);
        if (!this.new_draw_node) {
            this.new_draw_node = this.node.addComponent(cc.Graphics);
        }
        this.node.addChild(newNode);

        let paths = clip.curveData.paths;
        // console.log(paths);

        this.road_data_set = [];

        for (let k in paths) {
            let road_data = paths[k].props.position;
            this.gen_path_data(road_data);
        }
    }

    start() {

    }


    get_road_set() {
        return this.road_data_set;
    }

    gen_path_data(road_data) {
        let ctrl1 = null;
        let start_point = null;
        let end_point = null;
        let ctrl2 = null;

        let road_curve_path = []; // [start_point, ctrl1, ctrl2, end_point],
        for (let i = 0; i < road_data.length; i++) {
            let key_frame = road_data[i];
            if (ctrl1 !== null) {
                road_curve_path.push([start_point, ctrl1, ctrl1, cc.v2(key_frame.value[0], key_frame.value[1])]);
            }

            start_point = cc.v2(key_frame.value[0], key_frame.value[1]);

            for (let j = 0; j < key_frame.motionPath.length; j++) {
                let end = cc.v2(key_frame.motionPath[j][0], key_frame.motionPath[j][1]);
                ctrl2 = cc.v2(key_frame.motionPath[j][2], key_frame.motionPath[j][3]);
                if (ctrl1 === null) {
                    ctrl1 = ctrl2;
                }
                // 贝塞尔曲线 start_point, ctrl1, ctrl2, end_point,
                road_curve_path.push([start_point, ctrl1, ctrl2, end]);
                ctrl1 = cc.v2(key_frame.motionPath[j][4], key_frame.motionPath[j][5]);
                start_point = end;
            }
        }

        console.log(road_curve_path);

        let one_road = [road_curve_path[0][0]];

        for (let index = 0; index < road_curve_path.length; index++) {
            start_point = road_curve_path[index][0];
            ctrl1 = road_curve_path[index][1];
            ctrl2 = road_curve_path[index][2];
            end_point = road_curve_path[index][3];

            let len = this.bezier_length(start_point, ctrl1, ctrl2, end_point);
            let OFFSET = 16;
            let count = len / OFFSET;
            count = Math.floor(count);
            let t_delta = 1 / count;
            let t = t_delta;

            for (let i = 0; i < count; i++) {
                let x = start_point.x * (1 - t) * (1 - t) * (1 - t) + 3 * ctrl1.x * t * (1 - t) * (1 - t) + 3 * ctrl2.x * t * t * (1 - t) + end_point.x * t * t * t;
                let y = start_point.y * (1 - t) * (1 - t) * (1 - t) + 3 * ctrl1.y * t * (1 - t) * (1 - t) + 3 * ctrl2.y * t * t * (1 - t) + end_point.y * t * t * t;
                one_road.push(cc.v2(x, y));
                t += t_delta;
            }
        }

        console.log(one_road);
        if (this.is_debug) {
            this.new_draw_node.clear(); // 清除以前的

            for (let i = 0; i < one_road.length; i++) {
                this.new_draw_node.moveTo(one_road[i].x, one_road[i].y);
                this.new_draw_node.lineTo(one_road[i].x + 1, one_road[i].y + 1);
                this.new_draw_node.stroke();

                let node = new cc.Node('nodeName');
                let texture = new cc.Texture2D;
                let spriteFrame = new cc.SpriteFrame;
                texture.initWithData(new Uint8Array([0, 0, 0]), cc.Texture2D.PixelFormat.RGB888, 1, 1, 1);
                spriteFrame.setTexture(texture);
                spriteFrame.setRect(cc.rect(one_road[i].x + 1, one_road[i].y + 1, 20, 20));
                node.addComponent(cc.Sprite).spriteFrame = spriteFrame;

                this.node.addChild(node);
            }
        }

        this.road_data_set.push(one_road);
    }

    bezier_length(start_point, ctrl1, ctrl2, end_point) {
        // t [0, 1] t 分成20等分 1 / 20 = 0.05
        let prev_point = start_point;
        let length = 0;
        let t = 0.05;
        for (let i = 0; i < 20; i++) {
            let x = start_point.x * (1 - t) * (1 - t) * (1 - t) + 3 * ctrl1.x * t * (1 - t) * (1 - t) + 3 * ctrl2.x * t * t * (1 - t) + end_point.x * t * t * t;
            let y = start_point.y * (1 - t) * (1 - t) * (1 - t) + 3 * ctrl1.y * t * (1 - t) * (1 - t) + 3 * ctrl2.y * t * t * (1 - t) + end_point.y * t * t * t;
            let now_point = cc.v2(x, y);
            let dir = now_point.sub(prev_point);
            prev_point = now_point;
            length += dir.mag();

            t += 0.05;
        }
        return length;
    }

}